/* 
 * Copyright (c) 2015, S.F. Express Inc. All rights reserved.
 */
package com.sf.novatar.sample.slt;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.sf.integration.cas.cfgex.ICASSelector;
import com.sf.integration.cas.cfgex.ServerType;

/**
 * 描述：
 * <p>
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2015年11月13日      166046         Create
 * ****************************************************************************
 * </pre>
 *
 * @author 166046
 */
public class SrvSelector implements ICASSelector {
    static int i = 1;
    static Map<String, ServerType> types = new HashMap<>();

    @Override
    public ServerType getServerType(HttpServletRequest req) {
        String url = req.getRequestURL().toString();
        int idx = url.indexOf('/', url.indexOf("//") + 2);
        String sid = idx == -1 ? url : url.substring(0, idx);
        ServerType type = types.get(sid);
        if (type == null) {
            type = getType();
        }
        types.put(sid, type);
        return type;
    }

    private ServerType getType() {
        return (i++) % 2 == 0 ? ServerType.DEFAULT : ServerType.OTHER;
    }
}
